package com.leen.admin.controller;

import com.leen.admin.dto.MenuDTO;
import com.leen.admin.entity.Admin;
import com.leen.admin.service.MenuService;
import com.leen.admin.vo.menu.MenuVo;
import com.leen.common.vo.ResultVO;
import com.leen.security.CurrentAdminInfo;
import com.leen.security.domain.AdminLoginUserDetails;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 菜单 类说明
 *
 * @author admin
 * @date 2021-05-21
 */
@RestController
@RequestMapping(value = "/admin")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("获取前端所需菜单")
    @GetMapping(value = "/menus/build")
    public ResultVO<Object> buildMenus() {
        AdminLoginUserDetails adminLoginUserDetails = CurrentAdminInfo.getAdminLoginUserDetails();

        Admin admin = adminLoginUserDetails.getAdmin();
        Integer adminId = admin.getAdminId();

        List<MenuDTO> ownMenuList = this.menuService.findMenuByAdminId(adminId);

        List<MenuVo> menuVos = this.menuService.buildMenus(ownMenuList);
        return ResultVO.success(menuVos);
    }

    @ApiOperation("获取角色分配功能所需菜单")
    @GetMapping(value = "/menus/tree")
    public ResultVO<Object> buildMenusTree() {

        return ResultVO.success( this.menuService.buildMenuTree());
    }

    @ApiOperation("获取角色拥有菜单编号")
    @GetMapping(value = "/menus/own/{roleId}")
    public ResultVO<Set<Integer>> searchOwnMenuIdByRoleId( @PathVariable(value = "roleId") int roleId){
        return ResultVO.success( this.menuService.searchOwnMenuIdByRoleId(roleId));
    }
}
